import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
from random import choice

N = 150
my_font = fm.FontProperties(fname="/usr/share/fonts/truetype/arphic/ukai.ttc")
a = np.zeros((N, N))
b = []

for x in range(N):
    for y in range(N):
        b.append([x, y])

for P in np.arange(0,1,0.01):
    for i in range(N):
        for j in range(N):
            if a[i][j] == 10 or a[i][j] == 5:
                a[i][j] = 1
            else:
                continue

    for i in range(225):
        m = choice(b)
        b.remove(m)
        a[m[0],m[1]] = 1
        i = i+1
#得到团簇的坐标
    class P_change:
        def __init__(self, im):
            self.im = im
            m, n = im.shape
            self.mask = [[0 for _ in range(n)] for _ in range(m)]

        def num_change(self, i, j, mask, im):
            m, n = im.shape
            return i >= 0 and i < m and j >= 0 and j < n and mask[i][j] == 0 and im[i][j] == 1

        def add(self, i, j, mask, im, q):
            if self.num_change(i, j, mask, im):
                q.append([i, j])
                self.mask[i][j] = 1

        #遍历
        def DFS(self):
            m, n = self.im.shape
            tc = []
            for i in range(m):
                for j in range(n):
                    if self.mask[i][j] == 1 or self.im[i][j] == 0:
                        continue
                    P, Q = list(), list()
                    P.append([i, j])
                    self.mask[i][j] = 1
                    while P:
                        d = P.pop(0)
                        Q.append(d)
                        self.add(d[0] - 1, d[1], self.mask, a, P)
                        self.add(d[0] + 1, d[1], self.mask, a, P)
                        self.add(d[0], d[1] - 1, self.mask, a, P)
                        self.add(d[0], d[1] + 1, self.mask, a, P)
                    tc.append(Q)
            return tc
# 计算
    s = P_change(a)
    tc = s.DFS()
    max_tc_index = tc.index(max(tc, key=len))
    for m in tc[max_tc_index]:
        a[m[0]][m[1]] = 10
    middle = tc[max_tc_index]
    tc.remove(tc[max_tc_index])
    if tc == []:
        second_tc_index = 0
    else:
        second_tc_index = tc.index(max(tc, key=len))
        for m in tc[second_tc_index]:
            a[m[0]][m[1]] = 5
    # 将最大团簇的值返回存入
    tc.append(middle)

    # 绘图
    print(a)
    plt.figure(0,facecolor='blue')
    plt.imshow(a)
    plt.pause(0.01)
